package com.jbbmobile.hackathon.GWT.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.jbbmobile.hackathon.GWT.RPC.client.PersonService;
import com.jbbmobile.hackathon.GWT.RPC.client.PersonServiceAsync;

public class CreatePerson extends Composite {
	private static CreatePersonUiBinder uiBinder = GWT.create(CreatePersonUiBinder.class);
	interface CreatePersonUiBinder extends UiBinder<Widget, CreatePerson> {}

	@UiField TextBox name;
	@UiField TextBox phone;
	@UiField TextBox email;
	@UiField TextBox address;
	@UiField ListBox type;
	
	public CreatePerson() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	PersonServiceAsync personService = PersonService.Util.getInstance();
	
	@UiHandler("submit")
	void handleSubmit(ClickEvent e) {
		personService.createPerson(name.getText(), phone.getText(), email.getText(), 
				address.getText(), type.getSelectedIndex(), new AsyncCallback<Void>() {
					public void onFailure(Throwable caught) {	
						Window.alert( caught.getMessage() );
					}
					public void onSuccess(Void result) {
						name.setText("");
						phone.setText("");
						email.setText("");
						address.setText("");
					}			
		});
	}
	
}
